package com.tyg.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tyg.entity.NoticeInfo;
import com.tyg.service.NoticeInfoService;
import com.tyg.util.Constant;
import com.tyg.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-11-01
 * @RestController 是@controller和@ResponseBody 的结合
 * @ResponseBody 注解的作用是将 controller 的方法返回的对象通过适当的转换器转换为指定的格式之后
 * @RequestBody 作用在形参列表上时，用于将前台发送过来固定格式的数据
 * @Autowired常用来作属性的注入，可以作用在构造方法、普通方法、字段、注解、参数上。
 */
@RestController
@RequestMapping("/notice-info")
@CrossOrigin
@Slf4j
public class NoticeInfoController {

    @Autowired
    NoticeInfoService noticeService;

    //主页公告
    /**
     * @RequestParam用于将指定的请求参数赋值给方法中的形参。
     */
    @PostMapping("/getNoticeList")
    public ResultCode getNoticeList(@RequestParam(defaultValue = "1" ,required = true)int pageIndex, int pageSize){
        PageHelper.startPage(pageIndex, pageSize);
        List<NoticeInfo> noticeList= noticeService.getNoticeList();
        PageInfo<NoticeInfo> page= new PageInfo<NoticeInfo>(noticeList);
        return ResultCode.success(page);
    }

    //主页查看公告
    @PostMapping("/lookNotice")
    public ResultCode lookNotice(Integer id){
        NoticeInfo noticeInfo= noticeService.selectById(id);
        return ResultCode.success(noticeInfo);
    }

    /**
     * 公告设置
     */
    @PostMapping("/getNoticeListManager")
    public ResultCode getNoticeListManager(
            String noticeName,
            Integer status,
            String creatTime,
            @RequestParam(defaultValue = "1" ,required = true)int pageIndex, int pageSize){
        System.out.println(creatTime);
        //防止模糊查询时间后，去除时间后查询不到数据
        if (Constant.NULL_STRING.equals(creatTime)){
            creatTime = null;
        }
        PageHelper.startPage(pageIndex, pageSize);
        Map<String,Object> map= new HashMap<>();
        map.put("noticeName", noticeName);
        map.put("status", status);
        map.put("creatTime", creatTime);
        List<NoticeInfo> noticeListManager= noticeService.getNoticeListManager(map);
        PageInfo<NoticeInfo> page= new PageInfo<NoticeInfo>(noticeListManager);
        return ResultCode.success(page);
    }

    @PostMapping("/addNewNotice")
    public ResultCode addNewNotice(@RequestBody NoticeInfo notice)throws IllegalStateException {
        try {
            noticeService.insert(notice);
            return ResultCode.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "添加失败！");
        }
    }

    @PostMapping("/updateNotice")
    public ResultCode updateNotice(@RequestBody NoticeInfo notice){
        try {
            System.out.println(notice);
            noticeService.updateById(notice);
            return ResultCode.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "修改失败！");
        }
    }

    @PostMapping("/retract")
    public ResultCode retract(Integer id){
        System.out.println(id);
        NoticeInfo noticeInfo = noticeService.retract(id);
        return ResultCode.success(noticeInfo);
    }


}

